﻿@{
    Layout = null;
}
<style>
    .combo-panel {
  height:200px;
  overflow: auto;
}
</style>
<!--MaterialCatalog combobox begin-->
<select id="combo_materialCatalog_First" name="combo_materialCatalog_First" class="easyui-combobox" style="width:150px;"></select>
<select id="combo_materialCatalog_Second" name="combo_materialCatalog_Second" class="easyui-combobox" style="width:150px;"></select>
<select id="combo_materialCatalog_Third" name="combo_materialCatalog_Third" class="easyui-combobox" style="width:150px;"></select>
<input type="hidden" id="SelectedMaterialCatalogFirstId" name="SelectedMaterialCatalogFirstId" value="0">
<input type="hidden" id="SelectedMaterialCatalogSecondId" name="SelectedMaterialCatalogSecondId" value="0">
<input type="hidden" id="SelectedMaterialCatalogThirdId" name="SelectedMaterialCatalogThirdId" value="0">
<script type="text/javascript">
    // 一级类别变化
    function combo_MaterialCatalogFirst_Selected(record) {
        $('#SelectedMaterialCatalogFirstId').val(record.CatalogId);
        $("#combo_materialCatalog_Second").combobox("setValue", ''); //清空二级类别
        $("#combo_materialCatalog_Third").combobox("setValue", ''); //清空三级类别
        var firstid = $('#combo_materialCatalog_First').combobox('getValue');

        $.ajax({
            async: false,
            url: "/Material/GetCatalogByParentId",
            data: { parentId: firstid },
            type: "POST",
            dataType: "json",
            success: function (data) {
                /* alert(data);*/
                $("#combo_materialCatalog_Second").combobox("loadData", data);
            }

        });
    }
    function combo_MaterialCatalogFirst_Changed(nvalue, ovalue) {
        $('#SelectedMaterialCatalogFirstId').val(0);
    }

    //二级类别变化
    function combo_MaterialCatalogSecond_Selected(record) {
        $('#SelectedMaterialCatalogSecondId').val(record.CatalogId);
        $("#combo_materialCatalog_Third").combobox("setValue", ''); //清空三级类别
        var secondId = $('#combo_materialCatalog_Second').combobox('getValue');

        $.ajax({
            async: false,
            url: "/Material/GetCatalogByParentId",
            data: { parentId: secondId },
            type: "POST",
            dataType: "json",
            success: function (data) {
                $("#combo_materialCatalog_Third").combobox("loadData", data);
            }
        });
    }


    function combo_MaterialCatalogSecond_Changed(nvalue, ovalue) {
        $('#SelectedMaterialCatalogSecondId').val(0);
    }

    //三级类别变化
    function combo_MaterialCatalogThird_Selected(record) {
        $('#SelectedMaterialCatalogSecondId').val(record.CatalogId);
    }
    function combo_MaterialCatalogThird_Changed(nvalue, ovalue) {
        $('#SelectedMaterialCatalogThirdId').val(0);
    }

    function serachCatalog(list, id) {
        var catalog;
        $.each(list, function (i) {
            if (this.CatalogId == id) {
                catalog = this;
                return false;
            }
        });
        return catalog;
    }

    $(document).ready(function () {
        // 默认信息
        var defaultFirstId = parseInt("@ViewData["first"]");
        var defaultSecondId = parseInt("@ViewData["second"]");
        var defaultThirdId = parseInt("@ViewData["third"]");
        var IsNeedSetDefaultFirst = false;
        var IsNeedSetDefaultSecond = false;
        var IsNeedSetDefaultThird = false;
        if (defaultFirstId > 0) IsNeedSetDefaultFirst = true;
        if (defaultSecondId > 0) IsNeedSetDefaultSecond = true;
        if (defaultThirdId > 0) {
            IsNeedSetDefaultThird = true;
            IsNeedSetDefaultSecond = true;
            IsNeedSetDefaultFirst = true;
            $.ajax({
                async: false,
                url: "/Material/GetCatalogByThirdId",
                data: { thirdId: defaultThirdId },
                type: "POST",
                dataType: "json",
                success: function (data) {
                    defaultFirstId = data[0].CatalogId;
                    defaultSecondId = data[1].CatalogId;
                    defaultThirdId = data[2].CatalogId
                    //$("#combo_materialCatalog_Third").combobox("loadData", data);
                }
            });
        }



        $('#combo_materialCatalog_First').combobox({
            url: '/Material/GetAllFirstCatalog',
            method: 'post',
            valueField: 'CatalogId',
            textField: 'CatalogName',
            panelWidth: 150,
            panelHeight: 'auto',
            multiple: false,
            onSelect: combo_MaterialCatalogFirst_Selected,
            onChange: combo_MaterialCatalogFirst_Changed,
            onLoadSuccess: function (data) {
                if (defaultFirstId > 0 && IsNeedSetDefaultFirst) {
    
                    var firstCata = serachCatalog(data, defaultFirstId);
   
                    $('#combo_materialCatalog_First').combobox('setValue', firstCata.CatalogId);
                    combo_MaterialCatalogFirst_Selected(firstCata);
                    IsNeedSetDefaultFirst = false;
                }
                else {
                    $('#combo_materialCatalog_First').combobox('setValue', data[0].CatalogId);
                    combo_MaterialCatalogFirst_Selected(data[0]);
                }

            }
        });

        $('#combo_materialCatalog_Second').combobox({
            editable: false, //不可编辑状态
            cache: false,
            multiple: false,
            panelHeight: '150',//自动高度适合
            valueField: 'CatalogId',
            textField: 'CatalogName',
            onSelect: combo_MaterialCatalogSecond_Selected,
            onChange: combo_MaterialCatalogSecond_Changed,
            onLoadSuccess: function (data) {
                if (defaultSecondId > 0 && IsNeedSetDefaultSecond) {
                    var Cata = serachCatalog(data, defaultSecondId);
                    $('#combo_materialCatalog_Second').combobox('setValue', Cata.CatalogId);
                    combo_MaterialCatalogSecond_Selected(Cata);
                    IsNeedSetDefaultSecond = false;
                }
                else {
                    $('#combo_materialCatalog_Second').combobox('setValue', data[0].CatalogId);
                    combo_MaterialCatalogSecond_Selected(data[0]);
                }
            }
        });

        $('#combo_materialCatalog_Third').combobox({
            editable: false, //不可编辑状态
            cache: false,
            multiple: false,
            panelHeight: '150',//自动高度适合
            valueField: 'CatalogId',
            textField: 'CatalogName',
            onSelect: combo_MaterialCatalogThird_Selected,
            onChange: combo_MaterialCatalogThird_Changed,
            onLoadSuccess: function (data) {
                if (defaultThirdId > 0 && IsNeedSetDefaultThird) {
                    var Cata = serachCatalog(data, defaultThirdId);
                    $('#combo_materialCatalog_Third').combobox('setValue', Cata.CatalogId);
                    combo_MaterialCatalogThird_Selected(Cata);
                    IsNeedSetDefaultThird = false;
                }
                else {
                    $('#combo_materialCatalog_Third').combobox('setValue', data[0].CatalogId);
                    combo_MaterialCatalogThird_Selected(data[0]);
                }
            }
        });

    });
</script>
<!--MaterialCatalog combobox end-->
